package com.zygk.library.base;

import android.app.Activity;
import android.content.Context;
import android.graphics.BitmapFactory;
import android.graphics.PointF;
import android.net.Uri;
import android.os.AsyncTask;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import butterknife.BindView;
import butterknife.OnClick;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.SimpleTarget;
import com.davemorrissey.labs.subscaleview.ImageSource;
import com.davemorrissey.labs.subscaleview.ImageViewState;
import com.davemorrissey.labs.subscaleview.SubsamplingScaleImageView;
import com.zygk.library.R;
import com.zygk.library.R2;
import com.zygk.library.util.DensityUtil;
import com.zygk.library.util.ImageUtil;
import java.io.File;

import static com.davemorrissey.labs.subscaleview.SubsamplingScaleImageView.ZOOM_FOCUS_CENTER_IMMEDIATE;

public class BaseImageActivity extends BaseActivity {
  public static final String INTENT_URL = "INTENT_URL";
  public static final String INTENT_FROM_HOME = "INTENT_FROM_HOME";
  public static final String INTENT_TITLE = "INTENT_TITLE";
  @BindView(R2.id.iv) SubsamplingScaleImageView imageView;
  @BindView(R2.id.ll_back) LinearLayout llBack;
  @BindView(R2.id.lh_tv_title) TextView lhTvTitle;

  private String url, titlename;
  private Context mContext;
  private Activity mActivity;
  private boolean fromHome;
  @Override public void setContentView() {
    setContentView(R.layout.library_activity_base_image);
  }

  @Override public void init() {
    mContext = this;
    mActivity = this;
    url = getIntent().getStringExtra(INTENT_URL);
    fromHome = getIntent().getBooleanExtra(INTENT_FROM_HOME, false);
    titlename = getIntent().getStringExtra(INTENT_TITLE) == null ? ""
        : getIntent().getStringExtra(INTENT_TITLE);

    lhTvTitle.setText(titlename);
    llBack.setVisibility(View.VISIBLE);

    imageView.setMaxScale(15);
    imageView.setZoomEnabled(true);
    imageView.setMinimumScaleType(SubsamplingScaleImageView.SCALE_TYPE_CUSTOM);

    if(fromHome){
      showNoCancelPd();
      new AsyncTask<Void, Integer, Object>() {

        @Override
        protected Object doInBackground(Void... arg0) {
          try{
            return  ImageUtil.resourceToFile(mContext, R.mipmap.library_know_auto);
          }catch (Exception e){e.printStackTrace();}
          return  null;
        }

        @Override
        protected void onPostExecute(Object result) {
          try {
            dismissPd();
            if(result == null)
              return;
            File resource = (File) result;
            // 将保存的图片地址给SubsamplingScaleImageView,这里注意设置ImageViewState设置初始显示比例
            int sWidth = BitmapFactory.decodeFile(resource.getAbsolutePath()).getWidth();
            int sHeight = BitmapFactory.decodeFile(resource.getAbsolutePath()).getHeight();
            int width = DensityUtil.getWindowWidth(mActivity);
            int height = DensityUtil.getWindowHeight(mActivity);
            float scale = DensityUtil.getWindowWidth(mActivity) / (float) sWidth;
            if (sHeight >= height && sHeight / sWidth >= 3) {
              imageView.setMinimumScaleType(SubsamplingScaleImageView.SCALE_TYPE_CENTER_CROP);
              imageView.setImage(ImageSource.uri(Uri.fromFile(resource)),
                  new ImageViewState(scale, new PointF(0, 0), 0));
            } else {
              imageView.setMinimumScaleType(SubsamplingScaleImageView.SCALE_TYPE_CUSTOM);
              imageView.setImage(ImageSource.uri(Uri.fromFile(resource)));
              imageView.setDoubleTapZoomStyle(ZOOM_FOCUS_CENTER_IMMEDIATE);
            }
          } catch (Exception e) {
            e.printStackTrace();
          }
        }

        @Override
        protected void onPreExecute() {
        }

      }.execute();
      return;
    }

    showPd();
    Glide.with(mActivity)
        .load(url)
        .downloadOnly(new SimpleTarget<File>(){
          @Override
          public void onResourceReady(File resource, GlideAnimation<? super File> glideAnimation) {
            dismissPd();
            // 将保存的图片地址给SubsamplingScaleImageView,这里注意设置ImageViewState设置初始显示比例
            int sWidth = BitmapFactory.decodeFile(resource.getAbsolutePath()).getWidth();
            int sHeight = BitmapFactory.decodeFile(resource.getAbsolutePath()).getHeight();
            int width = DensityUtil.getWindowWidth(mActivity);
            int height = DensityUtil.getWindowHeight(mActivity);
            float scale = DensityUtil.getWindowWidth(mActivity) / (float) sWidth;
            if (sHeight >= height && sHeight / sWidth >= 3) {
              imageView.setMinimumScaleType(SubsamplingScaleImageView.SCALE_TYPE_CENTER_CROP);
              imageView.setImage(ImageSource.uri(Uri.fromFile(resource)),
                  new ImageViewState(scale, new PointF(0, 0), 0));
            } else {
              imageView.setMinimumScaleType(SubsamplingScaleImageView.SCALE_TYPE_CUSTOM);
              imageView.setImage(ImageSource.uri(Uri.fromFile(resource)));
              imageView.setDoubleTapZoomStyle(ZOOM_FOCUS_CENTER_IMMEDIATE);
            }
          }});
  }

  @OnClick(R2.id.ll_back) public void onViewClicked() {
    finish();
  }
}
